from flask import Blueprint,request,jsonify
from flask import current_app as app
import os
from db import db1
from utils.myfile import myf
from xlrd import open_workbook
bp_company=Blueprint('company',__name__)

@bp_company.route('/test')
async def test():
    str = '请求IP'+request.remote_addr+',请求的url'+request.url+'错误信息'+'\n'
    app.logger.error(str)
    return 'ok'


@bp_company.route('/upload',methods=['POST'])
def upload():
    # 获取文件
    file = request.files['file']
    print(file)
    newname = myf.rename(file.filename)
    # 上传
    file.save(os.path.join('./static/files/'+newname))
    # 打开文件读取
    name = './static/files/'+newname
    bk = open_workbook(name, encoding_override='utf-8')
    sh = bk.sheets()[0]
    nrows = sh.nrows
    ncols = sh.ncols
    for i in range(2, nrows):
        row_date = sh.row_values(i)
        # print(row_date[0])
        # print(row_date[1])
        for j in range(0,ncols):
            print(row_date[j])
    return 'ok'



@bp_company.route('/organization')
def organization():
    sql = "select * from organization"
    res = db1.findall(sql)
    return  jsonify({'code':200,'list':res})